///////////////////////////////////////////////////////////
// 
//  MyBox.cpp
// 
//  Authors:  Jeremy Bennett
//
///////////////////////////////////////////////////////////

// Local Includes
#include "stdafx.h"
#include "MyBox.h"
#include "MyContext.h"

// SDL Includes
#include <SDL_opengl.h>

// Bullet Includes
#include <BulletCollision/CollisionShapes/btBoxShape.h>
#include <LinearMath/btDefaultMotionState.h>

// System Includes

//GL_ShapeDrawer *g_pBltGLDrawer = new GL_ShapeDrawer();
//g_pBltGLDrawer->enableTexture(true);

MyBox::MyBox( const btVector3 &vSize, 
              const btVector3 &vOrigin, 
              const btScalar   fMass ) 
: MyShape() 
{
    _eType = my_box;

    _pShp = new btBoxShape(vSize);
    
    btTransform colTran;
	colTran.setIdentity();
    colTran.setOrigin(vOrigin);

    btVector3 locInertia(0,0,0);
    if ( fMass != 0 ) {
         _pShp->calculateLocalInertia(fMass,locInertia);
    }

    btDefaultMotionState* pMotState = new btDefaultMotionState(colTran);
    btRigidBody::btRigidBodyConstructionInfo rbInfo(fMass,pMotState,_pShp,locInertia);
	rbInfo.m_friction = 2.0f;
    setupRigidBody(rbInfo);

    if ( fMass != 0 ) {
        setLinearFactor(btVector3(1,1,0));
	    setAngularFactor(btVector3(0,0,1));
	    setCcdMotionThreshold(1.);
	    setCcdSweptSphereRadius(0.2f);
    }
}

MyBox::~MyBox() 
{
}

bool 
MyBox::render( MyContext *ctx )
{
    //return true;
    btScalar	m[16];

    btDefaultMotionState *pMotState = (btDefaultMotionState*)getMotionState();
    pMotState->m_graphicsWorldTrans.getOpenGLMatrix(m);

    glEnable(GL_COLOR_MATERIAL);
    glColor4f( _vCol[0], _vCol[1], _vCol[2], _vCol[3] );

#if 1
    glPushMatrix(); 
	glMultMatrixf(m);

	const btBoxShape* boxShape = static_cast<const btBoxShape*>(_pShp);
	btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
	
	static int indices[36] = {
			0,1,2,
			3,2,1,
			4,0,6,
			6,0,2,
			5,1,4,
			4,1,0,
			7,3,1,
			7,1,5,
			5,4,7,
			7,4,6,
			7,2,3,
			7,6,2};

        glColorMask( false, false, false, false );

		static btVector3 vertices[8]={	btVector3(1,1,1),btVector3(-1,1,1),	btVector3(1,-1,1),	btVector3(-1,-1,1),	btVector3(1,1,-1),	btVector3(-1,1,-1),	btVector3(1,-1,-1),	btVector3(-1,-1,-1)};
		glBegin (GL_TRIANGLES);
		int si=36;
		for (int i=0;i<si;i+=3)
		{
			btVector3 v1 = vertices[indices[i]]*halfExtent;
			btVector3 v2 = vertices[indices[i+1]]*halfExtent;
			btVector3 v3 = vertices[indices[i+2]]*halfExtent;
			btVector3 normal = (v3-v1).cross(v2-v1);
			normal.normalize ();

			glNormal3f(normal.getX(),normal.getY(),normal.getZ());
			glVertex3f (v1.x(), v1.y(), v1.z());
			glVertex3f (v2.x(), v2.y(), v2.z());
			glVertex3f (v3.x(), v3.y(), v3.z());
			
		}
		glEnd();

        glColorMask( true, true, true, true );

        glPopMatrix();
#else
    MyImage *pTex = (MyImage*)ctx->texture(_texID);

    glPushMatrix(); 
	glMultMatrixf(m);

    if (pTex) pTex->bindTexture();
    if (pTex) pTex->enableTexture();

	const btBoxShape* boxShape = static_cast<const btBoxShape*>(_pShp);
	btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();
	
	static int indices[36] = {
			0,1,2, 3,2,1,   // Front
			4,0,6, 2,6,0,   // Right
			5,1,4, 0,4,1,   // Top
			1,5,3, 7,3,5,   // Left
            5,4,7, 6,7,4,   // Back
			3,7,2, 6,2,7 }; // Bottom

	static btVector3 vertices[8]={	btVector3( 1, 1, 1),  // 
                                    btVector3(-1, 1, 1),  // 
                                    btVector3( 1,-1, 1),  // 
                                    btVector3(-1,-1, 1),  //
                                    btVector3( 1, 1,-1),  // 
                                    btVector3(-1, 1,-1),  // 
                                    btVector3( 1,-1,-1),  // 
                                    btVector3(-1,-1,-1)}; //

    static float texCoords[8] = { 1.0f, 1.0f,
                                  0.0f, 1.0f,
                                  1.0f, 0.0f,
                                  0.0f, 0.0f };

    float *tex1 = &texCoords[0];
    float *tex2 = &texCoords[2];
    float *tex3 = &texCoords[4];
    float *tex4 = &texCoords[6];

	glBegin (GL_TRIANGLES);
	int si=36;
	for (int i=0;i<si;i+=6)
	{
		btVector3 v1 = vertices[indices[i]]*halfExtent;
		btVector3 v2 = vertices[indices[i+1]]*halfExtent;
		btVector3 v3 = vertices[indices[i+2]]*halfExtent;
        btVector3 v4 = vertices[indices[i+3]]*halfExtent;
		btVector3 normal = (v3-v1).cross(v2-v1);
        normal*=-1;
		normal.normalize ();

		glNormal3f(normal.getX(),normal.getY(),normal.getZ());
        glTexCoord2fv(tex1);
		glVertex3f (v1.x(), v1.y(), v1.z());
        glTexCoord2fv(tex2);
		glVertex3f (v2.x(), v2.y(), v2.z());
        glTexCoord2fv(tex3);
		glVertex3f (v3.x(), v3.y(), v3.z());

        glTexCoord2fv(tex4);
		glVertex3f (v4.x(), v4.y(), v4.z());
        glTexCoord2fv(tex3);
		glVertex3f (v3.x(), v3.y(), v3.z());
        glTexCoord2fv(tex2);
		glVertex3f (v2.x(), v2.y(), v2.z());	
	}
	glEnd();

    if (pTex) pTex->disableTexture();

    glPopMatrix();
#endif

    return true;
}